define(["jQuery", "kendo","fileUpload","widget"], function ($, kendo,fileUpload,widget) {
    return {
		uploadResultUrl:false,
		
		show : function(){
			// $( "#uplimg-dragable-menu").offset({ top: App.lastClickGlobalPoint.y, left: App.lastClickGlobalPoint.x});
			var control = $("#fileupload");
			control.replaceWith( control = control.clone( true ) );
			var center = App.getAbsoluteCenter();
			$( "#uplimg-dragable-menu").css('left',center.x);
			$( "#uplimg-dragable-menu").css('top', center.y);
			
			$( "#uplimg-dragable-menu").show();
			
			$("#img_upl_loading").hide();
			$("#file_upload_result").html("");
			$("#save_img_upl_button").hide();
			 $('#fileupload').fileupload({
				dataType: 'json',
				maxFileSize: 5000000,
				autoUpload: true,
				replaceFileInput: false,
				acceptFileTypes: /(\.|\/)(gif|jpeg|jpg|png)$/i,
				add: function(e, data){
				
					$("#img_upl_loading").show();
					// $("#file_upload_result").html(data.files[0].name);
					$("#save_img_upl_button").hide();
					
					var fileExt = data.files[0].name.split('.');
					fileExt = fileExt[fileExt.length-1]; 
					var lowerfileExt = fileExt.toLowerCase();
					if(lowerfileExt != "gif" && lowerfileExt != "jpeg" && lowerfileExt != "jpg" && lowerfileExt != "png"  && lowerfileExt != "bmp")
					{ 	
						$("#file_upload_result").html('');	
						$("#img_upl_loading").hide();
						alert(App.localize("%only_image_warning"));
						return;
					}
					
					data.submit();
				},
				always: function(e, data) {
						$("#img_upl_loading").hide();
						$("#save_img_upl_button").show(); 
						var result;
						if (data.textStatus == 'parsererror') 
						{  // IE9 fails on upload's JSON response
							result = JSON.parse(data.jqXHR.responseText);
						} else if (data.textStatus == 'success') 
						{
							result = data.result;
						} 
						if (result) {
							// ...perform custom handling... 
							App.upload.uploadResultUrl = result.files[0].url;
						}
						console.log(result);
					}
					
								  
				});	
			
		},
		
        init: function (e) {
			$("#uplimg-dragable-menu").draggable();
			$("#uplimg-dragable-menu").hide();
			
		
			$(".uplimg-tools").click(function() {
				if(!App.toolboxValidation("tool_upload_img_ico"))
				{
					return
				}
				$(this).addClass('active');
				App.openedTool = $(this);
				App.setEnableImageCreationMode(true);
			});
				
		
			$(".save-img-button").click(function() {
				if(App.upload.uploadResultUrl)
				{
					$("#file_upload_result").html('');
					App.openedTool.removeClass('active');
					App.openedTool =false;
					$( "#uplimg-dragable-menu" ).hide();
					
					var center = App.getAbsoluteCenter();
					App.addUploadedImage(App.upload.uploadResultUrl,center.x,center.y);
					App.setEnableImageCreationMode(false);
				}
				else
				{
					$("#file_upload_result").html('');	
					$("#img_upl_loading").hide();
					alert(App.localize("%not_image_warning"));
				}
					
			});
			
			$(".cancel-button-img").click(function() {
				var r=confirm(App.localize("%cancel_confirmation"));
				if (r==true)
				{
					$("#file_upload_result").html('');			
					App.openedTool.removeClass('active');
					App.openedTool =false;
					$( "#uplimg-dragable-menu" ).hide();
					App.setEnableImageCreationMode(false,"");
				}
				else
				{
					console.log("You pressed Cancel!");
				}
			});
			
			
			
		
		}
	}
});